<?php



namespace app\mini\model;



use think\facade\Db;

use app\common\traits\ActionRecordTrait;

use think\Model;

use app\mini\model\SysModelMini;



class PersonService extends Model

{

	use ActionRecordTrait;



    /**

     * 表名

     * @var string

     */

    protected $name = 'service';

    protected $deleteTime = 'deleted_at';

    protected $defaultSoftDelete = 0;



    /**

     * 获取服务

     * @param  array $request 请求参数

     * @return array

     * @author hz

     * @create 2021-10-19

     */

    public function getServiceList($request)

    {

        try {

            $person_id = $request['person_id'] ?? 0;

            // $person_id = 29;

            $service_id = $request['service_id'] ?? 0;

            if(!$person_id)

            {

            	return ['error' => '您还未认证,请先认证'];

            }

            

            // 从服务记录进入服务，需要服务id

            if(!$service_id)

            {

                return ['error' => '参数错误'];

            }

            $where   = [];

            $where = [

                ['id', '=', $service_id]

            ];

            // $field = ['*'];

            $serviceInfo  = Db::name('service')->where($where)->find();

            if($serviceInfo)

            {

                $serviceInfo['start_time'] = $serviceInfo['start_time'] ? date('Y-m-d',$serviceInfo['start_time']) : '';

                $serviceInfo['end_time'] = $serviceInfo['end_time'] ? date('Y-m-d',$serviceInfo['end_time']) : '';

            }

            else{

                return ['error' => '该服务不存在'];

            }



            return ['data' => $serviceInfo];

        } catch (\Exception $e) {

            return ['error' => $e->getMessage()];

        }

    }



    /**

     * 获取服务记录列表

     * @param  array $request 请求参数

     * @return array

     * @author hz

     * @create 2021-10-19

     */

    public function getServiceLogList($request)

    {

        try {

            $person_id = $request['person_id'] ?? 0;

            // $person_id = 29;

            if(!$person_id)

            {

                return ['error' => '您还未认证,请先认证'];

            }

            $where   = [];

            $where = [

                ['nurse_id_one | nurse_id_two', '=', $person_id]

            ];



            $field = ['create_time,nurse_id_one,nurse_id_two,service_id'];

            $list = Db::name('service_log')->field($field)->where($where)->order('id DESC')->select()->toArray();



            $dataCount = Db::name('service_log')->where($where)->count();



            if ($list) 

            {

                // 护理师

                $nurse_id_one = array_column($list, 'nurse_id_one');

                $nurse_id_two = array_column($list, 'nurse_id_two');

                $nurse_id_one = is_array($nurse_id_one) ? $nurse_id_one : [];

                $nurse_id_two = is_array($nurse_id_two) ? $nurse_id_two : [];

                $nurse_ids = array_unique(array_merge($nurse_id_one,$nurse_id_two));

                $pwhere = [];

                $pwhere = [

                    ['id', 'in', $nurse_ids]

                ];

                $nurse_name = Db::name('shop_person')->where($pwhere)->column('name','id');



                foreach($list as &$v){

                    $v['create_time'] = $v['create_time'] ? date('Y-m-d H:i:s',$v['create_time']) : '';

                    $v['nurse_name_one'] = $nurse_name[$v['nurse_id_one']] ?? '';

                    $v['nurse_name_two'] = $nurse_name[$v['nurse_id_two']] ?? '';

                    

                }

            }



            $data              = [];

            $data['list']      = $list ?? [];

            $data['dataCount'] = $dataCount ?? 0;

            return ['data' => $data];

        } catch (\Exception $e) {

            return ['error' => $e->getMessage()];

        }

    }

    public function getList($request)
    {
        // echo "<pre>";
        // print_r($request);
        // echo "</pre>";

        try {

            $customerId = $request['id'] ?? '';

            $type = $request['type'] ?? '';
            
            $user_id = $request['user_id'] ?? '';

            $shopIds = $request['shopIds'] ?? '';

            $where = [];

            array_push($where,['status','=','1']);

            // array_push($where,['deleted_at','null','']);

            if($customerId>0){
                array_push($where,['customer_id','=',$customerId]);
            }

            if($type==1){
                $sysModel = new SysModelMini();

                $permissions = $sysModel->getPermissions($request);

                // $permissions = false;

                if($permissions){
                    // echo '范围权限';

                    array_push($where,['shop_id','in',$shopIds]);
                }else{
                    // echo '个人权限';

                    $shop_person_id = Db::name('admin_user')->where('id',$user_id)->value('shop_person_id');

                    $csutomerIds = Db::name('customer')->where('nurse_id','=',$shop_person_id)->column('id');

                    array_push($where,['customer_id','in',$csutomerIds]);
                }
                // exit;
            }

            $dataCount =  Db::name('service')->where($where)->count();     

            $serviceList = Db::name('service')->where($where)->limit(($request['page'] - 1) * $request['limit'], $request['limit'])->order('id DESC')->select()->toArray();

            $cid    = array_values(array_unique(array_column($serviceList, 'customer_id')));

            $customerInfo = Db::name('customer')->where('id','in',$cid)->column('customer_name,telephone','id');

            // echo "<pre>";
            // print_r($serviceList);
            // echo "</pre>";
            // exit;

            foreach ($serviceList as $k => $v) {
                $serviceList[$k]['service_type'] = $v['service_type'] == 1 ? '疗程卡' : '次卡';
                $serviceList[$k]['start_time'] = $v['start_time']>0 ? date("Y-m-d",$v['start_time']) : '';
                $serviceList[$k]['end_time'] = $v['end_time']>0 ? date("Y-m-d",$v['end_time']) : '';

                $serviceLogNum = Db::name('service_log')->where('service_id',$v['id'])->where('deleted_at','null','')->count();

                $serviceList[$k]['useNumber'] = $serviceLogNum;

                $serviceList[$k]['customer_name'] = $customerInfo[$v['customer_id']]['customer_name'];

                $serviceList[$k]['telephone'] = substr_replace($customerInfo[$v['customer_id']]['telephone'], '****', 3, 4);

            }


            $data              = [];

            $data['list']      = $serviceList ?? [];

            $data['dataCount'] = $dataCount ?? 0;

            return ['data' => $data];
        } catch (\Throwable $e) {
            return ['error' => $e->getMessage()];
        }
    }



}